package cn.zero.letaomallspringboot.controller;

import cn.zero.letaomallspringboot.Constant.ResultCode;
import cn.zero.letaomallspringboot.common.PageResult;
import cn.zero.letaomallspringboot.common.Result;
import cn.zero.letaomallspringboot.entity.ProductSpec;
import cn.zero.letaomallspringboot.service.IProductSpecService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productSpec")
public class ProductSpecController {

    private final IProductSpecService productSpecService;
    private static final Logger logger = LoggerFactory.getLogger(ProductSpecController.class);

    public ProductSpecController(IProductSpecService productSpecService) {
        this.productSpecService = productSpecService;
    }

    /**
     * 分页查询商品规格
     */
    @GetMapping("/list")
    public Result<PageResult<ProductSpec>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        logger.info("分页查询商品规格：{} to {}", page, size);
        PageResult<ProductSpec> result = productSpecService.pageQuery(page, size);
        return Result.success(result);
    }

    /**
     * 查询所有商品规格（不分页）
     */
    @GetMapping("/listAll")
    public Result<List<ProductSpec>> listAll() {
        logger.info("查询所有商品规格");
        List<ProductSpec> list = productSpecService.listAll();
        return Result.success(list);
    }

    /**
     * 根据商品ID查询规格
     */
    @GetMapping("/by-product/{productId}")
    public Result<List<ProductSpec>> getByProductId(@PathVariable Long productId) {
        logger.info("根据商品ID查询规格：{}", productId);
        List<ProductSpec> specs = productSpecService.getByProductId(productId);
        return Result.success(specs);
    }

    /**
     * 根据ID查询商品规格
     */
    @GetMapping("/{id}")
    public Result<ProductSpec> getById(@PathVariable Long id) {
        logger.info("根据ID查询商品规格：{}", id);
        ProductSpec spec = productSpecService.getById(id);
        if (spec == null) {
            return Result.fail(ResultCode.PRODUCT_SPEC_NOT_FOUND);
        }
        return Result.success(spec);
    }

    /**
     * 新增商品规格
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody ProductSpec productSpec) {
        logger.info("新增商品规格：{}", productSpec);
        boolean success = productSpecService.save(productSpec);
        return success ? Result.success(true) : Result.fail(ResultCode.PRODUCT_SPEC_INSERT_FAIL);
    }

    /**
     * 更新商品规格
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody ProductSpec productSpec) {
        logger.info("更新商品规格：{}", productSpec);
        // 先判断规格是否存在
        ProductSpec existSpec = productSpecService.getById(productSpec.getId());
        if (existSpec == null) {
            return Result.fail(ResultCode.PRODUCT_SPEC_NOT_FOUND);
        }
        boolean success = productSpecService.update(productSpec);
        return success ? Result.success(true) : Result.fail(ResultCode.PRODUCT_SPEC_UPDATE_FAIL);
    }

    /**
     * 删除商品规格
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        logger.info("删除商品规格：{}", id);
        // 先判断规格是否存在
        ProductSpec existSpec = productSpecService.getById(id);
        if (existSpec == null) {
            return Result.fail(ResultCode.PRODUCT_SPEC_NOT_FOUND);
        }
        boolean success = productSpecService.delete(id);
        return success ? Result.success(true) : Result.fail(ResultCode.PRODUCT_SPEC_DELETE_FAIL);
    }
}